package com.example.testhowareyou1;

import java.util.*;

import android.app.*;
import android.content.*;
import android.graphics.drawable.*;
import android.os.*;
import android.support.v4.view.*;
import android.view.*;
import android.widget.*;

import com.actionbarsherlock.app.*;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.devspark.sidenavigation.*;
import com.devspark.sidenavigation.SideNavigationView.Mode;

public class MainActivity extends SherlockFragmentActivity implements
ISideNavigationCallback {

	private ActionBar actionBar;
	private SideNavigationView sideNavigationView;
	private ImageView sideMenuIcon;

	private ViewPager bioViewPager;
	private ViewPager holoViewPager;
	private ViewPager bloodViewPager;


	private TextView specialDate;
	int mYear;
	int mMonth;
	int mDay;
	Calendar todayDate = Calendar.getInstance();
	final static int DIALOG_DATE=0;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// side bar
		sideNavigationView = (SideNavigationView) findViewById(R.id.side_navigation_view);
		sideNavigationView.setMenuItems(R.menu.side_navigation_menu);
		sideNavigationView.setMenuClickCallback(this);

		// action bar
		actionBar = getSupportActionBar();
		actionBar.setBackgroundDrawable(new ColorDrawable(0x00000000));
		actionBar.setTitle("How Do You Feel?");
		actionBar.setSubtitle("I'm fine,thank you");
		actionBar.setLogo(R.drawable.blue_action_side_navigation);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);

		// bio ViewPager
		bioViewPager = (ViewPager) findViewById(R.id.bioViewPager);
		bioViewPager.setAdapter(new BioViewPagerAdapter(getApplicationContext()));

		// holo ViewPager
		holoViewPager = (ViewPager) findViewById(R.id.holoViewPager);
		holoViewPager.setAdapter(new HoloViewPagerAdapter(getApplicationContext()));

		// blood ViewPager
		bloodViewPager = (ViewPager) findViewById(R.id.bloodViewPager);
		bloodViewPager.setAdapter(new BloodViewPagerAdapter(getApplicationContext()));

		// special date
		specialDate=(TextView)findViewById(R.id.specialDate);
		specialDate.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String[] dateSubMenu_Items = { "Today", "Special Day", "D-Day" };
				AlertDialog.Builder dateSubMenu = new AlertDialog.Builder(
						MainActivity.this);
				dateSubMenu.setIcon(R.drawable.ic_launcher);
				dateSubMenu.setTitle("Set the date up");

				dateSubMenu.setSingleChoiceItems(dateSubMenu_Items, 0, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						switch (which) {
						case 0:
							specialDate.setText("오늘은  "+todayDate.get(Calendar.YEAR)+"년 "
									+(todayDate.get(Calendar.MONTH)+1)+"월 "
									+todayDate.get(Calendar.DAY_OF_YEAR)+"일 입니다.");
							dialog.dismiss();
							break;
						case 1:
							dialog.dismiss();
							showDialog(DIALOG_DATE);

							break;
						case 2:
							dialog.dismiss();
							showDialog(DIALOG_DATE);
							break;
						}
					}
				});
				dateSubMenu.create();
				dateSubMenu.show();
			}
		});
	}

	DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {

			specialDate.setText(year + "년" + (monthOfYear + 1) + "월" + dayOfMonth
					+ "일");
		}
	};

	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {

		todayDate = Calendar.getInstance();

		mYear = todayDate.get(Calendar.YEAR);
		mMonth = todayDate.get(Calendar.MONTH);
		mDay = todayDate.get(Calendar.DAY_OF_MONTH);

		return new DatePickerDialog(this, dateListener, mYear, mMonth, mDay);
	}

	private View.OnClickListener mPagerListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.biobtn_to_after:
				setCurrentInflateItem(1);
				break;
			case R.id.biobtn_to_before:
				setCurrentInflateItem(2);
				break;

			case R.id.holobtn_to_after:
				setCurrentInflateItem(3);
				break;

			case R.id.holobtn_to_before:
				setCurrentInflateItem(4);
				break;

			case R.id.bloodbtn_to_after:
				setCurrentInflateItem(5);
				break;

			case R.id.bloodbtn_to_before:
				setCurrentInflateItem(6);
				break;
			}
		}
	};

	private void setCurrentInflateItem(int type) {

		if (type == 1) {
			bioViewPager.setCurrentItem(1);
		} else if (type == 2) {
			bioViewPager.setCurrentItem(0);
		} else if (type == 3) {
			holoViewPager.setCurrentItem(1);
		} else if (type == 4) {
			holoViewPager.setCurrentItem(0);
		} else if (type == 5) {
			bloodViewPager.setCurrentItem(1);
		} else if (type == 6) {
			bloodViewPager.setCurrentItem(0);
		}
	}

	/**
	 * BioViewPagerAdapter
	 */
	class BioViewPagerAdapter extends PagerAdapter {

		private LayoutInflater myInflater1;

		public BioViewPagerAdapter(Context c) {
			super();
			myInflater1 = LayoutInflater.from(c);
		}

		@Override
		public int getCount() {
			return 2;
		}

		@Override
		public Object instantiateItem(View pager, int position) {
			View v = null;
			if (position == 0) {
				v = myInflater1.inflate(R.layout.bio_viewpager_before, null);
				v.findViewById(R.id.biobtn_to_after).setOnClickListener(
						mPagerListener);
			} else if (position == 1) {
				v = myInflater1.inflate(R.layout.bio_viewpager_after, null);
				v.findViewById(R.id.biobtn_to_before).setOnClickListener(
						mPagerListener);
			}

			((ViewPager) pager).addView(v, 0);

			return v;
		}

		@Override
		public void destroyItem(View pager, int position, Object view) {
			((ViewPager) pager).removeView((View) view);
		}

		@Override
		public boolean isViewFromObject(View pager, Object obj) {
			return pager == obj;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}

		@Override
		public void finishUpdate(View arg0) {
		}

	}

	/**
	 * HoloViewPagerAdapter
	 */
	class HoloViewPagerAdapter extends PagerAdapter {

		private LayoutInflater myInflater1;

		public HoloViewPagerAdapter(Context c) {
			super();
			myInflater1 = LayoutInflater.from(c);
		}

		@Override
		public int getCount() {
			return 2;
		}

		@Override
		public Object instantiateItem(View pager, int position) {
			View v = null;
			if (position == 0) {
				v = myInflater1.inflate(R.layout.holo_viewpager_before, null);
				v.findViewById(R.id.holobtn_to_after).setOnClickListener(
						mPagerListener);
			} else if (position == 1) {
				v = myInflater1.inflate(R.layout.holo_viewpager_after, null);
				v.findViewById(R.id.holobtn_to_before).setOnClickListener(
						mPagerListener);
			}

			((ViewPager) pager).addView(v, 0);

			return v;
		}

		@Override
		public void destroyItem(View pager, int position, Object view) {
			((ViewPager) pager).removeView((View) view);
		}

		@Override
		public boolean isViewFromObject(View pager, Object obj) {
			return pager == obj;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}

		@Override
		public void finishUpdate(View arg0) {
		}

	}

	/**
	 * BloodViewPagerAdapter
	 */
	class BloodViewPagerAdapter extends PagerAdapter {

		private LayoutInflater myInflater1;

		public BloodViewPagerAdapter(Context c) {
			super();
			myInflater1 = LayoutInflater.from(c);
		}

		@Override
		public int getCount() {
			return 2;
		}

		@Override
		public Object instantiateItem(View pager, int position) {
			View v = null;
			if (position == 0) {
				v = myInflater1.inflate(R.layout.blood_viewpager_before, null);
				v.findViewById(R.id.bloodbtn_to_after).setOnClickListener(
						mPagerListener);
			} else if (position == 1) {
				v = myInflater1.inflate(R.layout.blood_viewpager_after, null);
				v.findViewById(R.id.bloodbtn_to_before).setOnClickListener(
						mPagerListener);
			}

			((ViewPager) pager).addView(v, 0);

			return v;
		}

		@Override
		public void destroyItem(View pager, int position, Object view) {
			((ViewPager) pager).removeView((View) view);
		}

		@Override
		public boolean isViewFromObject(View pager, Object obj) {
			return pager == obj;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}

		@Override
		public void finishUpdate(View arg0) {
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_main, menu);
		if (sideNavigationView.getMode() == Mode.RIGHT) {
			menu.findItem(R.id.mode_right).setChecked(true);
		} else {
			menu.findItem(R.id.mode_left).setChecked(true);
		}

		menu.add("CROP_ICON").setIcon(R.drawable.ic_launcher)
		.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		menu.add("CROP_TITLE").setTitle("CROP")
		.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		menu.add("SEND_ICON").setIcon(R.drawable.ic_launcher)
		.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		menu.add("SEND_TITLE").setTitle("SEND")
		.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			sideNavigationView.toggleMenu();
			break;
		case R.id.mode_left:
			item.setChecked(true);
			sideNavigationView.setMode(Mode.LEFT);
			break;
		case R.id.mode_right:
			item.setChecked(true);
			sideNavigationView.setMode(Mode.RIGHT);
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	@Override
	public void onSideNavigationItemClick(int itemId) {
		// TODO Auto-generated method stub
		switch (itemId) {
		case R.id.side_navigation_menu_item1:
			setTitle("menu1");
			sideMenuIcon.setImageResource(R.drawable.ic_launcher);
			break;
		case R.id.side_navigation_menu_item2:
			setTitle("menu2");
			sideMenuIcon.setImageResource(R.drawable.ic_launcher);
			break;
		case R.id.side_navigation_menu_item3:
			setTitle("menu3");
			sideMenuIcon.setImageResource(R.drawable.ic_launcher);
			break;
		case R.id.side_navigation_menu_item4:
			setTitle("menu4");
			sideMenuIcon.setImageResource(R.drawable.ic_launcher);
			break;
		case R.id.side_navigation_menu_item5:
			setTitle("menu5");
			sideMenuIcon.setImageResource(R.drawable.ic_launcher);
			break;
		default:
			break;
		}

	}

}
